package org.nowcoder.leetcode.arrays;


import java.util.Arrays;

/**
 * Title  : 169. Majority Element
 * Source : https://leetcode.cn/problems/majority-element/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-06
 */

public class LeetCode_169 {
    public int majorityElement(int[] nums) {
        //暴力解法： 打擂台
//        int ele = 0, count = 0;
//        for (int num : nums) {
//            int tmp = 0;
//            for (int i : nums) {
//                if (num == i) {
//                    tmp++;
//                }
//            }
//            if(tmp > (nums.length/2))return num;
//            if (count < tmp) {
//                ele = num;
//                count = tmp;
//            }
//        }
//        return ele;

        //排序： nums.length / 2 一定是多数元素
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }

}
